using System;
using System.Collections.Generic;
using System.Text;

using Weazel.Microblaze.Simulator.Engine;

namespace Weazel.Microblaze.Simulator.Components.Microblaze.Instructions
{
    /// <summary>
    /// Sign extend byte
    /// </summary>
    public class Sext8 : Instruction
    {
        public override void Set(uint address, uint value)
        {
            base.Set(address, value);

            if (ImmediateValue != 0x60)
                throw new UnknownInstructionException(value);
        }

        public override int CycleCount
        {
            get { return 1; }
        }

        public Sext8()
            : base("sext8")
        {
        }

        /// <summary>
        /// This instruction sign-extends a byte (8 bits) into a word (32 bits). 
        /// Bit 24 in rA will be copied into bits 0-23 of rD. Bits 24-31 in rA 
        /// will be copied into bits 24-31 of rD.
        /// </summary>    
        public override void Execute(MicroblazeComponent MicroblazeComponent)
        {
            uint ra = MicroblazeComponent.ReadRegister(SourceRegisterA);

            uint rd = (uint)(((int)ra << 24) >> 24);

            MicroblazeComponent.WriteRegister(DestinationRegister, rd, Address);
        }

        public override string ToString()
        {
            return string.Format("sext8 r{0}, r{1}",
              DestinationRegister,
              SourceRegisterA
            );
        }
    }
}
